# !/bin/sh
# COPYRIGHT lipujun Equipment CO.,LTD
# file name: boot.sh
# function: This script is used to boot
#
# author: lipujun

source /AIR_FLASH/script/air.conf
source /AIR_FLASH/system/air_sys.conf

#check HWVersion
linuxName=`uname -r`
if [ "${linuxName}" = "4.19.68-dirty" ] ; then #qiantu
    hardVersion="V0"    
elif [ "${linuxName}" = "4.19.68" ] ; then #diange
    hardVersion="V1"
fi
if [ -z "${HWVersion}" ] ; then
    echo "HWVersion=${hardVersion}" >> /AIR_FLASH/script/air.conf
    source /AIR_FLASH/script/air.conf
elif [ "${HWVersion}" != "${hardVersion}" ] ; then
    sed -i -e "s|HWVersion=${HWVersion}|HWVersion=${hardVersion}|" /AIR_FLASH/script/air.conf
    source /AIR_FLASH/script/air.conf
fi

#check network config
if [ "${HWVersion}" == "V0" ] ; then #qiantu
    if [ "${GE1}" != "eth0" ] ; then
        sed -i -e "s|GE1=${GE1}|GE1=eth0|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${GE2}" != "eth1" ] ; then
        sed -i -e "s|GE2=${GE2}|GE2=eth1|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${GE3}" != "eth2" ] ; then
        sed -i -e "s|GE3=${GE3}|GE3=eth2|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${GE4}" != "eth3" ] ; then
        sed -i -e "s|GE4=${GE4}|GE4=eth3|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${POE1}" != "eth5" ] ; then
        sed -i -e "s|POE1=${POE1}|POE1=eth5|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${POE2}" != "eth4" ] ; then
        sed -i -e "s|POE2=${POE2}|POE2=eth4|" /AIR_FLASH/system/air_sys.conf
    fi
elif [ "${HWVersion}" == "V1" ] ; then #diange
    if [ "${GE1}" != "eth3" ] ; then
        sed -i -e "s|GE1=${GE1}|GE1=eth3|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${GE2}" != "eth4" ] ; then
        sed -i -e "s|GE2=${GE2}|GE2=eth4|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${GE3}" != "eth5" ] ; then
        sed -i -e "s|GE3=${GE3}|GE3=eth5|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${GE4}" != "eth6" ] ; then
        sed -i -e "s|GE4=${GE4}|GE4=eth6|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${POE1}" != "eth1" ] ; then
        sed -i -e "s|POE1=${POE1}|POE1=eth1|" /AIR_FLASH/system/air_sys.conf
    fi
    if [ "${POE2}" != "eth2" ] ; then
        sed -i -e "s|POE2=${POE2}|POE2=eth2|" /AIR_FLASH/system/air_sys.conf
    fi
fi
source /AIR_FLASH/script/air.conf
source /AIR_FLASH/system/air_sys.conf
$prefix/script/poe.sh on
echo "config ****************************************network"
$prefix/script/ip.sh   



#qiantu 
if [ "${HWVersion}" == "V0" ] ; then
    echo 1 > /sys/block/sda/device/queue_depth
fi
#check part table and create parts
if [ ${dataCollect_usb} -gt 0 ] ; then #usb collect
    ret=`fdisk -l |  grep -v Disk | grep -v Device| grep -v Units| grep -v Sector| grep -v size| grep -v size|grep sda | wc -l`
    if [ $ret -ne 2 ] ; then
        if [ -z "${usbCollect_6APartSize}" ] ; then
            echo "usbCollect_6APartSize=64G" >> /AIR_FLASH/script/air.conf
	    source /AIR_FLASH/script/air.conf
        fi
	echo "USB fdisk SSD fdisk_sda.sh  ${usbCollect_6APartSize}"
        /AIR_FLASH/script/fdisk_sda.sh ${usbCollect_6APartSize}
    else
        if [ -z "${usbCollect_6APartSize}" ] ; then
            echo "usbCollect_6APartSize=64G" >> /AIR_FLASH/script/air.conf
	    source /AIR_FLASH/script/air.conf
        fi
        usbPartSize=`fdisk -l |grep sda1 | awk '{print $5}'`
        if [ "${usbPartSize}" != "${usbCollect_6APartSize}" ] ; then
	    echo "usbCollect_6APartSize=${usbCollect_6APartSize} not ${usbPartSize} refdisk,fdisk_sda.sh ${usbCollect_6APartSize}"
            /AIR_FLASH/script/fdisk_sda.sh ${usbCollect_6APartSize}
     	fi
    fi
elif [ ${dataCollect_usb} -eq 0 ] ; then # VAC collect
    ret=`fdisk -l |  grep -v Disk | grep -v Device| grep -v Units| grep -v Sector| grep -v size| grep -v size|grep sda | wc -l`
    if [ $ret -ne 1 ] ; then
	 echo "VAC fdisk SSD fdisk_sda.sh"
         /AIR_FLASH/script/fdisk_sda.sh
    fi
fi
#mount part and repair
rm /6A -fr
if [ ${dataCollect_usb} -gt 0 ] ; then #usb collect
    #repair 6A
    mkdir -p /6A
    umount /dev/sda1
    dosfsck -a /dev/sda1
    if [ "${HWVersion}" == "V0" ] ; then
        mount -o codepage=936,iocharset=utf8 /dev/sda1 /6A
    elif [ "${HWVersion}" == "V1" ] ; then
        mount /dev/sda1 /6A
    fi
    rm -f /6A/FSCK*
    rm -f /6A/fsck*
    #repair SATA
    mkdir -p /upload
    umount /dev/sda2
    fsck.ext4 -a /dev/sda2

    mount /dev/sda2 /upload
    rm -f /upload/FSCK*
    rm -f /upload/fsck*
elif [ ${dataCollect_usb} -eq 0 ] ; then # VAC collect
    #repair SATA
    mkdir -p /upload
    umount /dev/sda1
    fsck.ext4 -a /dev/sda1
    mount /dev/sda1 /upload
    rm -f /upload/FSCK*
    rm -f /upload/fsck*       
fi
#check ftpd and start
echo "\n`date +%Y-%m-%d` `date +%H:%M:%S` @@@@@@@@@@@@@@@@@@@@system POWER UP now"
if [  -f "/AIR_FLASH/script/ftpUser.sh" ] ; then
    /AIR_FLASH/script/ftpUser.sh
else
    vsftpd -v
    if [ $? -ne 0 ] ; then
        if [ "${HWVersion}" == "V0" ] ; then
            tcpsvd 0 21 ftpd -w /upload &
        fi
    fi
fi
#start application
if [[ -f "${prefix}/script/bondCheck.sh" ]] && [[ "${Netbond}" == "bond0" ]] ; then
    echo "config ****************************************network******check**POE1***POE2"
    $prefix/script/bondCheck.sh &
fi
echo "start *****************************************boot.sh"
$prefix/air_flash -c $prefix/script/air.conf &
if [ -f "${prefix}/script/usb.sh" ] ; then 
    echo "start *****************************************usb.sh"
    $prefix/script/usb.sh &
fi
sleep 3
$prefix/script/monitor.sh &
cd /
